package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.user.CaptchaException;
import com.ruoyi.common.exception.user.CaptchaExpireException;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.RegisterComuserMapper;
import com.ruoyi.system.domain.RegisterComuser;
import com.ruoyi.system.service.IRegisterComuserService;
import com.ruoyi.system.manager.AsyncManager;
import com.ruoyi.system.manager.factory.AsyncFactory;
/**
 * 公司用户注册Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-06-19
 */
@Service
public class RegisterComuserServiceImpl implements IRegisterComuserService 
{
    @Autowired
    private RegisterComuserMapper registerComuserMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询公司用户注册
     * 
     * @param id 公司用户注册主键
     * @return 公司用户注册
     */
    @Override
    public RegisterComuser selectRegisterComuserById(Long id)
    {
        return registerComuserMapper.selectRegisterComuserById(id);
    }

    /**
     * 查询公司用户注册列表
     * 
     * @param registerComuser 公司用户注册
     * @return 公司用户注册
     */
    @Override
    public List<RegisterComuser> selectRegisterComuserList(RegisterComuser registerComuser)
    {
        return registerComuserMapper.selectRegisterComuserList(registerComuser);
    }

    /**
     * 新增公司用户注册
     * 
     * @param registerComuser 公司用户注册
     * @return 结果
     */
    @Override
    public int insertRegisterComuser(RegisterComuser registerComuser)
    {
        return registerComuserMapper.insertRegisterComuser(registerComuser);
    }

    /**
     * 修改公司用户注册
     * 
     * @param registerComuser 公司用户注册
     * @return 结果
     */
    @Override
    public int updateRegisterComuser(RegisterComuser registerComuser)
    {
        return registerComuserMapper.updateRegisterComuser(registerComuser);
    }

    /**
     * 批量删除公司用户注册
     * 
     * @param ids 需要删除的公司用户注册主键
     * @return 结果
     */
    @Override
    public int deleteRegisterComuserByIds(Long[] ids)
    {
        return registerComuserMapper.deleteRegisterComuserByIds(ids);
    }

    /**
     * 删除公司用户注册信息
     * 
     * @param id 公司用户注册主键
     * @return 结果
     */
    @Override
    public int deleteRegisterComuserById(Long id)
    {
        return registerComuserMapper.deleteRegisterComuserById(id);
    }

    /**
     * 注册
     */
    public String register(RegisterComuser registerBody)
    {
        String msg = "", username = registerBody.getLoginName(), password = registerBody.getPassword();
        SysUser sysUser = new SysUser();
        sysUser.setUserName(username);
        registerBody.setStatus(0L);
        // 验证码开关
        boolean captchaEnabled = configService.selectCaptchaEnabled();
//        if (captchaEnabled)
//        {
//            validateCaptcha(username, registerBody.getCode(), registerBody.getUuid());
//        }
        if (StringUtils.isEmpty(username))
        {
            msg = "用户名不能为空";
        }
        else if (StringUtils.isEmpty(password))
        {
            msg = "用户密码不能为空";
        }
        else if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH)
        {
            msg = "账户长度必须在2到20个字符之间";
        }
        else if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
        {
            msg = "密码长度必须在5到20个字符之间";
        }
        else if (!userService.checkUserNameUnique(sysUser))
        {
            msg = "保存用户'" + username + "'失败，注册账号已存在";
        }
        else
        {
            registerBody.setPassword(SecurityUtils.encryptPassword(password));

            boolean regFlag = insertRegisterComuser(registerBody)>0;
            if (!regFlag)
            {
                msg = "注册失败,请联系系统管理人员";
            }
            else
            {
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success")));
            }
        }
        return msg;
    }

    /**
     * 校验验证码
     *
     * @param username 用户名
     * @param code 验证码
     * @param uuid 唯一标识
     * @return 结果
     */
    public void validateCaptcha(String username, String code, String uuid)
    {
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(uuid, "");
        String captcha = redisCache.getCacheObject(verifyKey);
        redisCache.deleteObject(verifyKey);
        if (captcha == null)
        {
            throw new CaptchaExpireException();
        }
        if (!code.equalsIgnoreCase(captcha))
        {
            throw new CaptchaException();
        }
    }
}
